
clear
set more off

set matsize 5000
set more off

********************************************************************************
*                      MARRIAGES: MAIN ESTIMATES                               *
********************************************************************************


***   1.   NATIVE HUSBAND - FOREIGN WIFE   ***

use "$data\gains_marriage_nativehusband.dta" , replace 

egen newEUxPostEU=rmax(eu2004post2004 eu2007post2007)
gen wbprov = wbc*1000+provres
gen yearprov = year*1000+provres

gen lnF=ln(Fsingle)
gen lnM=ln(Msingle)
gen lnFsingle_for_hbc=ln(Fsingle_for_hbc)
gen lnMsingle_for_wbc=ln(Msingle_for_wbc)
gen newEUxlnF=(eu2004==1|eu2007==1)*lnF
gen newEUxlnM=(eu2004==1|eu2007==1)*lnM
gen newEUxlnFsingle_for_hbc=(eu2004==1|eu2007==1)*lnFsingle_for_hbc
gen newEUxlnMsingle_for_wbc=(eu2004==1|eu2007==1)*lnMsingle_for_wbc
tab wbc, gen(dwbc)
forval i=1/159 {
gen wbcyear`i' = dwbc`i'*year
}

tab provres, gen(dprov)
forval i=1/`r(r)' {
gen provtrend`i' = dprov`i'*year
}


********************************************************************************
*  Table 3 - Columns 1-3
*  Gains from marriage before and after the EU enlargements, DID estimates

areg phixy newEUxPostEU i.year i.provres [aw=population], absorb(wbc)  vce(cluster provres)
outreg2 using "$output\baseline.xls" , keep(newEUxPostEU) replace 
areg phixy newEUxPostEU i.year provtrend1-provtrend99 wbcyear1-wbcyear159 [aw=population], absorb(wbprov) vce(cluster provres)
outreg2 using "$output\baseline.xls" , keep(newEUxPostEU) append 
areg phixy_noirreg newEUxPostEU i.year provtrend1-provtrend99 wbcyear1-wbcyear159 [aw=population], absorb(wbprov) vce(cluster provres)
outreg2 using "$output\baseline.xls" , keep(newEUxPostEU) append 


********************************************************************************
*  Appendix Table A3 - Columns 1-3
*  Gains from marriage before and after the EU enlargements, DID estimates (non-weighted)

areg phixy newEUxPostEU i.year i.provres,   vce(cluster provres) absorb(wbc)
outreg2 using "$output\robustness_OLS.xls" , keep(newEUxPostEU) replace 
areg phixy newEUxPostEU i.year provtrend1-provtrend99 wbcyear1-wbcyear159, absorb(wbprov) vce(cluster provres)
outreg2 using "$output\robustness_OLS.xls" , keep(newEUxPostEU) append 
areg phixy_noirreg newEUxPostEU i.year provtrend1-provtrend99 wbcyear1-wbcyear159, absorb(wbprov) vce(cluster provres)
outreg2 using "$output\robustness_OLS.xls" , keep(newEUxPostEU) append 


********************************************************************************
*  Table 4 - Columns 1-4
*  Gains from marriage before and after the EU enlargements, DID estimates (robustness to controlling for the number of singles)

areg phixy newEUxPostEU lnM lnF i.year provtrend1-provtrend99 wbcyear1-wbcyear159 [aw=population] , absorb(wbprov) vce(cluster provres)
outreg2 using "$output\robustness_numbersingles.xls" , keep(newEUxPostEU lnF lnM) replace
areg phixy newEUxPostEU lnMsingle_for_wbc i.year provtrend1-provtrend99 wbcyear1-wbcyear159 [aw=population] , absorb(wbprov) vce(cluster provres)
outreg2 using "$output\robustness_numbersingles.xls" , keep(newEUxPostEU lnMsingle_for_wbc) append
areg phixy newEUxPostEU lnF lnM lnMsingle_for_wbc i.year provtrend1-provtrend99 wbcyear1-wbcyear159 [aw=population], absorb(wbprov) vce(cluster provres)
outreg2 using "$output\robustness_numbersingles.xls" , keep(newEUxPostEU lnF lnM lnMsingle_for_wbc) append
xi: areg phixy newEUxPostEU i.wbc*lnF i.wbc*lnM i.wbc*lnMsingle_for_wbc i.year provtrend1-provtrend99 wbcyear1-wbcyear159 [aw=population], absorb(wbprov) vce(cluster provres)
outreg2 using "$output\robustness_numbersingles.xls" , keep(newEUxPostEU) append


********************************************************************************
*  Appendix Table A5 - Columns 2 and 4
*  Gains from marriage before and after the EU enlargements, DID estimates (robustness to controlling for the number of singles OLS and IV)

* IV
gen lQM=ln(Qhmale)
gen lQF=ln(Qwfemale)
bys provres hbc year : gen laglQM= lQM[_n-1]
bys provres wbc year : gen laglQF= lQF[_n-1]
ivreg2 phixy newEUxPostEU (lnM lnF = laglQM laglQF) i.year provtrend1-provtrend99 wbcyear1-wbcyear159 [aw=population] , absorb(wbprov) vce(cluster provres)
outreg2 using "$output\robustness_numbersingles_iv.xls" , keep(newEUxPostEU lnF lnM) replace
ivreg2 phixy newEUxPostEU (lnM lnF = laglQM laglQF) lnMsingle_for_wbc i.year provtrend1-provtrend99 wbcyear1-wbcyear159 [aw=population], absorb(wbprov) vce(cluster provres)
outreg2 using "$output\robustness_numbersingles_iv.xls" , keep(newEUxPostEU lnF lnM lnMsingle_for_wbc) append


********************************************************************************
*  Figure 7 - Panel a. Native husband - foreign wife
* Changes in gains from marriage between natives and new EU citizens, year-specific estimates

global fully eu2004t1999 eu2004t2000 eu2004t2001 eu2004t2002 eu2004t2003 eu2004t2004 eu2004t2005 eu2004t2006 eu2004t2007 eu2004t2008 eu2004t2009 ///
eu2004t2010 eu2004t2011 eu2004t2012 eu2007t1999 eu2007t2000 eu2007t2001 eu2007t2002 eu2007t2003 eu2007t2004 eu2007t2005 eu2007t2006 eu2007t2007 eu2007t2008 eu2007t2009 eu2007t2010 ///
eu2007t2011 eu2007t2012
areg phixy $fully i.year provtrend1-provtrend99 wbcyear1-wbcyear159 [aw=population] , absorb(wbprov) vce(cluster provres)
outreg2 using "$output\fullyinteracted.xls" , keep(eu2004t* eu2007t*) replace


********************************************************************************
*  Appendix Table A4 - Columns 1-2 
*  Gains from marriage before and after the EU enlargements, DID estimates allowing for announcement effects and for substitution across nationalities

areg phixy eu2004post2003 eu2004post2004 eu2004post2006 eu2004post2007 eu2007post2003 eu2007post2004 eu2007post2006 eu2007post2007 i.year i.provres, absorb(wbc) vce(cluster provres)
outreg2 using "$output\announcements.xls" , keep(eu2004post* eu2007post*) replace
areg phixy eu2004post2003 eu2004post2004 eu2004post2006 eu2004post2007 eu2007post2003 eu2007post2004 eu2007post2006 eu2007post2007 i.year provtrend1-provtrend99 wbcyear1-wbcyear159, absorb(wbprov) vce(cluster provres)
outreg2 using "$output\announcements.xls" , keep(eu2004post* eu2007post*) append


* Merge with Internet data
* Source i.Stat data: Aspects of daily life survey -> http://dati.istat.it/#

preserve
import excel "$data\internet_use.xls", sheet("Sheet1") firstrow clear
keep if year<=2012
rename region REGION
rename codreg region
save "$data\internet.dta" , replace
restore

drop _merge
merge m:1 region year using "$data\internet.dta" , keep(matched)
tab _merge
drop _merge
rename onemore_w coverage
gen newEUxPostEUxcoverage= newEUxPostEU*coverage
gen lnFxcoverage=lnF*coverage
gen lnMxcoverage=lnM*coverage
gen newEUxcoverage= (eu2004==1|eu2007==1)*coverage
gen lnnm=ln(nm)


********************************************************************************
*  Appendix Table A7 - Columns 1-4
*  Internet diffusion and intermarriages

areg lnnm lnF i.year provtrend1-provtrend99 wbcyear1-wbcyear159 [w=population], abs(wbprov) vce(cluster provres)
outreg2 using "$output\internet.xls" , keep(lnF) replace 
areg lnnm lnF lnFxcoverage i.year provtrend1-provtrend99 wbcyear1-wbcyear159 [w=population], abs(wbprov) vce(cluster provres)
outreg2 using "$output\internet.xls" , keep(lnF lnFxcoverage) append
areg phixy newEUxPostEU i.year provtrend1-provtrend99 wbcyear1-wbcyear159 [aw=population] ,  vce(cluster provres) abs(wbprov)
outreg2 using "$output\internet.xls" , keep(newEUxPostEU) append 
areg phixy newEUxPostEU newEUxPostEUxcoverage i.year provtrend1-provtrend99 wbcyear1-wbcyear159 [aw=population] ,  vce(cluster provres) abs(wbprov)
outreg2 using "$output\internet.xls" , keep(newEUxPostEU newEUxPostEUxcoverage) append 




***   2. NATIVE HUSBAND - FOREIGN WIFE   ***

use "$data\gains_marriage_nativewife.dta" , replace 

egen newEUxPostEU=rmax(eu2004post2004 eu2007post2007)
gen hbprov = hbc*1000+provres
gen yearprov = year*1000+provres

gen lnF=ln(Fsingle)
gen lnM=ln(Msingle)
gen lnFsingle_for_hbc=ln(Fsingle_for_hbc)
gen lnMsingle_for_wbc=ln(Msingle_for_wbc)
gen newEUxlnF=(eu2004==1|eu2007==1)*lnF
gen newEUxlnM=(eu2004==1|eu2007==1)*lnM
gen newEUxlnFsingle_for_hbc=(eu2004==1|eu2007==1)*lnFsingle_for_hbc
gen newEUxlnMsingle_for_wbc=(eu2004==1|eu2007==1)*lnMsingle_for_wbc
codebook hbc
tab hbc, gen(dhbc)
forval i=1/153 {
gen hbcyear`i' = dhbc`i'*year
}
tab provres, gen(dprov)
forval i=1/`r(r)' {
gen provtrend`i' = dprov`i'*year
}

********************************************************************************
*  Table 3 - Columns 4-6
*  Gains from marriage before and after the EU enlargements, DID estimates

areg phixy newEUxPostEU i.year i.provres [aw=population],  absorb(hbc) vce(cluster provres)
outreg2 using "$output\baseline.xls" , keep(newEUxPostEU) append 
areg phixy newEUxPostEU i.year provtrend1-provtrend99 hbcyear1-hbcyear153 [aw=population], absorb(hbprov) vce(cluster provres)
outreg2 using "$output\baseline.xls" , keep(newEUxPostEU) append 
areg phixy_noirreg newEUxPostEU i.year provtrend1-provtrend99 hbcyear1-hbcyear153 [aw=population], absorb(hbprov) vce(cluster provres)
outreg2 using "$output\baseline.xls" , keep(newEUxPostEU) append 


********************************************************************************
*  Appendix Table A3 - Columns 4-6
*  Gains from marriage before and after the EU enlargements, DID estimates (non-weighted)

areg phixy newEUxPostEU i.year i.provres,  absorb(hbc) vce(cluster provres)
outreg2 using "$output\robustness_OLS.xls" , keep(newEUxPostEU) append 
areg phixy newEUxPostEU i.year provtrend1-provtrend99 hbcyear1-hbcyear153, absorb(hbprov) vce(cluster provres)
outreg2 using "$output\robustness_OLS.xls" , keep(newEUxPostEU) append 
areg phixy_noirreg newEUxPostEU i.year provtrend1-provtrend99 hbcyear1-hbcyear153, absorb(hbprov) vce(cluster provres)
outreg2 using "$output\robustness_OLS.xls" , keep(newEUxPostEU) append 


********************************************************************************
*  Table 4 - Columns 5-8
*  Gains from marriage before and after the EU enlargements, DID estimates (robustness to controlling for the number of singles)

areg phixy newEUxPostEU lnM lnF i.year provtrend1-provtrend99 hbcyear1-hbcyear153 [aw=population] , absorb(hbprov) vce(cluster provres)
outreg2 using "$output\robustness_numbersingles.xls" , keep(newEUxPostEU lnF lnM) append
areg phixy newEUxPostEU lnFsingle_for_hbc i.year provtrend1-provtrend99 hbcyear1-hbcyear153 [aw=population] , absorb(hbprov) vce(cluster provres)
outreg2 using "$output\robustness_numbersingles.xls" , keep(newEUxPostEU lnFsingle_for_hbc) append
areg phixy newEUxPostEU lnF lnM lnFsingle_for_hbc i.year provtrend1-provtrend99 hbcyear1-hbcyear153 [aw=population], absorb(hbprov) vce(cluster provres)
outreg2 using "$output\robustness_numbersingles.xls" , keep(newEUxPostEU lnF lnM lnFsingle_for_hbc) append
xi: areg phixy newEUxPostEU i.hbc*lnF i.hbc*lnM i.hbc*lnFsingle_for_hbc i.year provtrend1-provtrend99 hbcyear1-hbcyear153 [aw=population], absorb(hbprov) vce(cluster provres)
outreg2 using "$output\robustness_numbersingles.xls" , keep(newEUxPostEU) append


********************************************************************************
*  Appendix Table A5 - Columns 6 and 8
*  Gains from marriage before and after the EU enlargements, DID estimates (robustness to controlling for the number of singles OLS and IV)

* IV
gen lQM=ln(Qhmale)
gen lQF=ln(Qwfemale)
bys provres hbc year : gen laglQM= lQM[_n-1]
bys provres wbc year : gen laglQF= lQF[_n-1]
ivreg phixy newEUxPostEU (lnM lnF = laglQM laglQF) i.year provtrend1-provtrend99 hbcyear1-hbcyear153 [aw=population] , absorb(hbprov) vce(cluster provres)
outreg2 using "$output\robustness_numbersingles_iv.xls" , keep(newEUxPostEU lnF lnM) append
ivreg phixy newEUxPostEU (lnM lnF = laglQM laglQF) lnFsingle_for_hbc i.year provtrend1-provtrend99 hbcyear1-hbcyear153 [aw=population], absorb(hbprov) vce(cluster provres)
outreg2 using "$output\robustness_numbersingles_iv.xls" , keep(newEUxPostEU lnF lnM lnFsingle_for_hbc) append


********************************************************************************
*  Figure 7 - Panel b. Native wife - foreign husband 
*  Changes in gains from marriage between natives and new EU citizens, year-specific estimates

global fully eu2004t1999 eu2004t2000 eu2004t2001 eu2004t2002 eu2004t2003 eu2004t2004 eu2004t2005 eu2004t2006 eu2004t2007 eu2004t2008 eu2004t2009 ///
eu2004t2010 eu2004t2011 eu2004t2012 eu2007t1999 eu2007t2000 eu2007t2001 eu2007t2002 eu2007t2003 eu2007t2004 eu2007t2005 eu2007t2006 eu2007t2007 eu2007t2008 eu2007t2009 eu2007t2010 ///
eu2007t2011 eu2007t2012
areg phixy $fully i.year provtrend1-provtrend99 hbcyear1-hbcyear153 [aw=population] , absorb(hbprov) vce(cluster provres)
outreg2 using "$output\fullyinteracted.xls" , keep(eu2004t* eu2007t*) append


********************************************************************************
*  Appendix Table A4 - Columns 3-4
*  Gains from marriage before and after the EU enlargements, DID estimates allowing for announcement effects and for substitution across nationalities

areg phixy eu2004post2003 eu2004post2004 eu2004post2006 eu2004post2007 eu2007post2003 eu2007post2004 eu2007post2006 eu2007post2007 i.year i.provres, absorb(hbc) vce(cluster provres)
outreg2 using "$output\announcements.xls" , keep(eu2004post* eu2007post*) append
areg phixy eu2004post2003 eu2004post2004 eu2004post2006 eu2004post2007 eu2007post2003 eu2007post2004 eu2007post2006 eu2007post2007 i.year provtrend1-provtrend99 hbcyear1-hbcyear153, absorb(hbprov) vce(cluster provres)
outreg2 using "$output\announcements.xls" , keep(eu2004post* eu2007post*) append


* Merge with Internet data
drop _merge
merge m:1 region year using "$data\internet.dta" , keep(matched)
tab _merge
drop _merge
rename onemore_w coverage
gen newEUxPostEUxcoverage= newEUxPostEU*coverage
gen newEUxcoverage= (eu2004==1|eu2007==1)*coverage
gen lnnm=ln(nm)
gen lnMxcoverage=lnM*coverage
gen lnFxcoverage=lnF*coverage


********************************************************************************
*  Appendix Table A7 - Columns 5-8
*  Internet diffusion and intermarriages

areg lnnm lnM i.year provtrend1-provtrend99 hbcyear1-hbcyear153 [w=population], abs(hbprov) vce(cluster provres)
outreg2 using "$output\internet.xls" , keep(lnM) append 
areg lnnm lnM lnMxcoverage i.year provtrend1-provtrend99 hbcyear1-hbcyear153 [w=population], abs(hbprov) vce(cluster provres)
outreg2 using "$output\internet.xls" , keep(lnM lnMxcoverage) append
areg phixy newEUxPostEU i.year provtrend1-provtrend99 hbcyear1-hbcyear153 [aw=population] ,  vce(cluster provres) abs(hbprov)
outreg2 using "$output\internet.xls" , keep(newEUxPostEU) append 
areg phixy newEUxPostEU newEUxPostEUxcoverage i.year provtrend1-provtrend99 hbcyear1-hbcyear153 [aw=population] ,  vce(cluster provres) abs(hbprov)
outreg2 using "$output\internet.xls" , keep(newEUxPostEU newEUxPostEUxcoverage) append 








********************************************************************************
*                      COHABITATIONS : ESTIMATES                               *
********************************************************************************


***   1.   NATIVE HUSBAND - FOREIGN WIFE   ***


*************************************************
*              ONLY MARRIAGES                   *

use "$data\Gain_marriage_robust.dta"  , clear
drop if year<=1997 
drop if year>=2003 & year<=2007
replace year=2001 if year<=2002
replace year=2011 if year>=2008
tab year
keep year hbc wbc provres region nm
collapse (sum) nm , by( hbc wbc provres year)
lab var nm "marriage"
preserve
gen type= "marriage"
save "$data\marriages_forcohab_tomergehetw.dta", replace
restore

merge m:1 provres hbc year using "$data\Msingle_cohabitation_FINAL.dta" , keep(match master) keepusing(Msingle)
tab _merge
drop _merge 
merge m:1 provres wbc year using "$data\Fsingle_cohabitation_FINAL.dta" , keep(match master) keepusing(Fsingle)
tab _merge
drop _merge 
merge m:1 provres using "$data\population_byprovince.dta" , keep(match master) 
tab _merge
drop _merge

gen phixy = ln((nm^2)/(Fsingle*Msingle))

* Treatment
keep if hbc==0
gen nat= wbc
merge m:1 nat using "$data\CodiciStatiEsteri.dta" , keepusing(EUII)
tab _merge
drop _merge
drop if year==.
replace EUII=4 if nat==224 
replace EUII=4 if nat==258 
replace EUII=4 if nat==259  
replace EUII=4 if nat==260  
replace EUII=4 if nat==261  
replace EUII=6 if nat==317  
replace EUII=0 if nat==0 
lab define eui3  0 "ITA" 1 "EU15"  2 "EU2004" 3 "EU2007" 4 "EU_Other" 5 "Africa" 6 "Asia" 7 "America_South" 8 "OECD" 
label values EUII eui3
  
tab EUII, gen(euii)
rename euii1 ita
rename euii2 eu15
rename euii3 eu2004
rename euii4 eu2007
rename euii5 euother
rename euii6 africa
rename euii7 asia
rename euii8 americas
rename euii9 oecd
codebook EUII
drop if EUII==.

// Post and Interaction 
gen post2004= (year>=2004)
gen post2007= (year>=2007)
gen eu2004post2004= eu2004*post2004
gen eu2007post2007= eu2007*post2007
drop if phixy==.
drop if phixy==0
xtset provres 
order provres year
sort provres year

gen eg=wbc
merge m:1 provres eg year using "$data\Msingle_cohabitation_FINAL_merge2.dta" , keep(match master) keepusing(Msingle_for_wbc)
tab _merge
drop _merge 
replace Msingle_for_wbc=round(Msingle_for_wbc,1)
gen lnF=ln(Fsingle)
gen lnM=ln(Msingle)
gen lnMsingle_for_wbc=ln(Msingle_for_wbc)

set matsize 5000
set more off
gen wbprov = wbc*1000+provres
egen newEUxPostEU=rmax(eu2004post2004 eu2007post2007)
gen yearprov = year*1000+provres
tab provres, gen(dprov)
forval i=1/`r(r)' {
gen provtrend`i' = dprov`i'*year
}

********************************************************************************
*  Table 5 - Column 1. Marriages 
*  Gains from marriage and cohabitation before and after the EU enlargements, DID estimates

areg phixy newEUxPostEU i.year provtrend1-provtrend`r(r)' [aw=population], absorb(wbprov) vce(cluster provres)
outreg2 using "$output\robustness_cohabitations.xls" , keep(newEUxPostEU) replace 



******************************************************
*               ONLY COHABITATIONS                   *

use "$data\Gain_cohabitation_robust.dta" , clear
keep if hbc==0

* Treatment
gen nat= wbc
merge m:1 nat using "$data\CodiciStatiEsteri.dta" , keepusing(EUII)
tab _merge
drop _merge
drop if year==.
replace EUII=4 if nat==224  
replace EUII=4 if nat==258  
replace EUII=4 if nat==259  
replace EUII=4 if nat==260  
replace EUII=4 if nat==261  
replace EUII=6 if nat==317  
replace EUII=0 if nat==0 
lab define eui3  0 "ITA" 1 "EU15"  2 "EU2004" 3 "EU2007" 4 "EU_Other" 5 "Africa" 6 "Asia" 7 "America_South" 8 "OECD" 
label values EUII eui3

tab EUII, gen(euii)
rename euii1 ita
rename euii2 eu15
rename euii3 eu2004
rename euii4 eu2007
rename euii5 euother
rename euii6 africa
rename euii7 asia
rename euii8 americas
rename euii9 oecd
codebook EUII
drop if EUII==.

*  Post and Interaction 
gen post2004= (year>=2004)
gen post2007= (year>=2007)
gen eu2004post2004= eu2004*post2004
gen eu2007post2007= eu2007*post2007
drop if phixy==.
drop if phixy==0
xtset provres 
order provres year
sort provres year
xtsum phixy 

gen eg=wbc
merge m:1 provres eg year using "$data\Msingle_cohabitation_FINAL_merge2.dta" , keep(match master) keepusing(Msingle_for_wbc)
tab _merge
drop _merge 
replace Msingle_for_wbc=round(Msingle_for_wbc,1)
gen lnF=ln(Fsingle)
gen lnM=ln(Msingle)
gen lnMsingle_for_wbc=ln(Msingle_for_wbc)

set matsize 5000
set more off
gen wbprov = wbc*1000+provres
egen newEUxPostEU=rmax(eu2004post2004 eu2007post2007)
gen yearprov = year*1000+provres
tab provres, gen(dprov)
forval i=1/`r(r)' {
gen provtrend`i' = dprov`i'*year
}

********************************************************************************
*  Table 5 - Column 2. Cohabitations 
*  Gains from marriage and cohabitation before and after the EU enlargements, DID estimates

areg phixy newEUxPostEU i.year provtrend1-provtrend`r(r)' [aw=population], absorb(wbprov) vce(cluster provres)
outreg2 using "$output\robustness_cohabitations.xls" , keep(newEUxPostEU) append 




**************************************************
*         COHABITATIONS + MARRIAGES              * 

use "$data\Gain_marriage_robust.dta"  , clear
drop if year<=1997 
drop if year>=2003 & year<=2007
replace year=2001 if year<=2002
replace year=2011 if year>=2008
tab year
keep year hbc wbc provres region nm
append using "$data\cohabitations_tomerge.dta"

collapse (sum) nm , by( hbc wbc provres year)
lab var nm "marriage+cohabitation"

merge m:1 provres hbc year using "$data\Msingle_cohabitation_FINAL.dta" , keep(match master) keepusing(Msingle)
tab _merge
drop _merge 
merge m:1 provres wbc year using "$data\Fsingle_cohabitation_FINAL.dta" , keep(match master) keepusing(Fsingle)
tab _merge
drop _merge 
merge m:1 provres using "$data\population_byprovince.dta" , keep(match master) 
tab _merge
drop _merge

gen phixy = ln((nm^2)/(Fsingle*Msingle))

* Treatment
keep if hbc==0
gen nat= wbc
merge m:1 nat using "$data\CodiciStatiEsteri.dta" , keepusing(EUII)
tab _merge
drop _merge
drop if year==.
replace EUII=4 if nat==224 
replace EUII=4 if nat==258  
replace EUII=4 if nat==259  
replace EUII=4 if nat==260  
replace EUII=4 if nat==261  
replace EUII=6 if nat==317  
replace EUII=0 if nat==0 
lab define eui3  0 "ITA" 1 "EU15"  2 "EU2004" 3 "EU2007" 4 "EU_Other" 5 "Africa" 6 "Asia" 7 "America_South" 8 "OECD" 
label values EUII eui3
  
tab EUII, gen(euii)
rename euii1 ita
rename euii2 eu15
rename euii3 eu2004
rename euii4 eu2007
rename euii5 euother
rename euii6 africa
rename euii7 asia
rename euii8 americas
rename euii9 oecd
codebook EUII
drop if EUII==.

* Post and Interaction 
gen post2004= (year>=2004)
gen post2007= (year>=2007)
gen eu2004post2004= eu2004*post2004
gen eu2007post2007= eu2007*post2007
drop if phixy==.
drop if phixy==0
xtset provres 
order provres year
sort provres year

gen eg=wbc
merge m:1 provres eg year using "$data\Msingle_cohabitation_FINAL_merge2.dta" , keep(match master) keepusing(Msingle_for_wbc)
tab _merge
drop _merge 
replace Msingle_for_wbc=round(Msingle_for_wbc,1)
gen lnF=ln(Fsingle)
gen lnM=ln(Msingle)
gen lnMsingle_for_wbc=ln(Msingle_for_wbc)

set matsize 5000
set more off
gen wbprov = wbc*1000+provres
egen newEUxPostEU=rmax(eu2004post2004 eu2007post2007)
gen yearprov = year*1000+provres
tab provres, gen(dprov)
forval i=1/`r(r)' {
gen provtrend`i' = dprov`i'*year
}


********************************************************************************
*  Table 5 - Column 3. Marriages and Cohabitations
*  Gains from marriage and cohabitation before and after the EU enlargements, DID estimates

areg phixy newEUxPostEU i.year provtrend1-provtrend`r(r)' [aw=population], absorb(wbprov) vce(cluster provres)
outreg2 using "$output\robustness_cohabitations.xls" , keep(newEUxPostEU) append 






***   2.   NATIVE WIFE-FOREIGN HUSBAND   ***

*************************************************
*              ONLY MARRIAGES                   *

use "$data\Gain_marriage_robust.dta"  , clear
drop if year<=1997 
drop if year>=2003 & year<=2007
replace year=2001 if year<=2002
replace year=2011 if year>=2008
tab year
keep year hbc wbc provres region nm
collapse (sum) nm , by( hbc wbc provres year)
lab var nm "marriage"
preserve
gen type= "marriage"
save "$data\marriages_forcohab_tomerge.dta", replace
restore

merge m:1 provres hbc year using "$data\Msingle_cohabitation_FINAL.dta" , keep(match master) keepusing(Msingle)
tab _merge
drop _merge 
merge m:1 provres wbc year using "$data\Fsingle_cohabitation_FINAL.dta" , keep(match master) keepusing(Fsingle)
tab _merge
drop _merge 
merge m:1 provres using "$data\population_byprovince.dta" , keep(match master) 
tab _merge
drop _merge

gen phixy = ln((nm^2)/(Fsingle*Msingle))

* Treatment
keep if wbc==0
gen nat= hbc
merge m:1 nat using "$data\CodiciStatiEsteri.dta" , keepusing(EUII)
tab _merge
drop _merge
drop if year==.
replace EUII=4 if nat==224 
replace EUII=4 if nat==258  
replace EUII=4 if nat==259  
replace EUII=4 if nat==260  
replace EUII=4 if nat==261  
replace EUII=6 if nat==317  
replace EUII=0 if nat==0 
lab define eui3  0 "ITA" 1 "EU15"  2 "EU2004" 3 "EU2007" 4 "EU_Other" 5 "Africa" 6 "Asia" 7 "America_South" 8 "OECD" 
label values EUII eui3
  
tab EUII, gen(euii)
rename euii1 ita
rename euii2 eu15
rename euii3 eu2004
rename euii4 eu2007
rename euii5 euother
rename euii6 africa
rename euii7 asia
rename euii8 americas
rename euii9 oecd
codebook EUII
drop if EUII==.

*  Post and Interaction 
gen post2004= (year>=2004)
gen post2007= (year>=2007)
gen eu2004post2004= eu2004*post2004
gen eu2007post2007= eu2007*post2007
drop if phixy==.
drop if phixy==0
xtset provres 
order provres year
sort provres year

gen eg=hbc
merge m:1 provres eg year using "$data\Fsingle_cohabitation_FINAL_merge2.dta" , keep(match master) keepusing(Fsingle_for_hbc)
tab _merge
drop _merge 
replace Fsingle_for_hbc=round(Fsingle_for_hbc,1)
gen lnF=ln(Fsingle)
gen lnM=ln(Msingle)
gen lnFsingle_for_hbc=ln(Fsingle_for_hbc)

set matsize 5000
set more off
gen hbprov = hbc*1000+provres
egen newEUxPostEU=rmax(eu2004post2004 eu2007post2007)
gen yearprov = year*1000+provres

tab provres, gen(dprov)
forval i=1/`r(r)' {
gen provtrend`i' = dprov`i'*year
}


********************************************************************************
*  Table 5 - Column 4. Marriages 
*  Gains from marriage and cohabitation before and after the EU enlargements, DID estimates

areg phixy newEUxPostEU i.year provtrend1-provtrend`r(r)' [aw=population] , absorb(hbprov) vce(cluster provres)
outreg2 using "$output\robustness_cohabitations.xls" , keep(newEUxPostEU) append 




******************************************************
*               ONLY COHABITATIONS                   *

use "$data\Gain_cohabitation_robust.dta" , clear
keep if wbc==0

* Treatment
gen nat= hbc
merge m:1 nat using "$data\CodiciStatiEsteri.dta" , keepusing(EUI EUII)
tab _merge
drop _merge
drop if year==.
replace EUII=4 if nat==224  
replace EUII=4 if nat==258  
replace EUII=4 if nat==259  
replace EUII=4 if nat==260  
replace EUII=4 if nat==261  
replace EUII=6 if nat==317  
replace EUII=0 if nat==0 
lab define eui3  0 "ITA" 1 "EU15"  2 "EU2004" 3 "EU2007" 4 "EU_Other" 5 "Africa" 6 "Asia" 7 "America_South" 8 "OECD" 
label values EUII eui3
  
tab EUII, gen(euii)
rename euii1 ita
rename euii2 eu15
rename euii3 eu2004
rename euii4 eu2007
rename euii5 euother
rename euii6 africa
rename euii7 asia
rename euii8 americas
rename euii9 oecd
codebook EUII
drop if EUII==.

*  Post and Interaction 
gen post2004= (year>=2004)
gen post2007= (year>=2007)
gen eu2004post2004= eu2004*post2004
gen eu2007post2007= eu2007*post2007
drop if phixy==.
drop if phixy==0
xtset provres 
order provres year
sort provres year
xtsum phixy 

gen eg=hbc
merge m:1 provres eg year using "$data\Fsingle_cohabitation_FINAL_merge2.dta" , keep(match master) keepusing(Fsingle_for_hbc)
tab _merge
drop _merge 
replace Fsingle_for_hbc=round(Fsingle_for_hbc,1)
gen lnF=ln(Fsingle)
gen lnM=ln(Msingle)
gen lnFsingle_for_hbc=ln(Fsingle_for_hbc)

set matsize 5000
set more off
gen hbprov = hbc*1000+provres
egen newEUxPostEU=rmax(eu2004post2004 eu2007post2007)
gen yearprov = year*1000+provres
tab provres, gen(dprov)
forval i=1/`r(r)' {
gen provtrend`i' = dprov`i'*year
}

********************************************************************************
*  Table 5 - Column 5. Cohabitations
*  Gains from marriage and cohabitation before and after the EU enlargements, DID estimates

areg phixy newEUxPostEU i.year provtrend1-provtrend`r(r)' [aw=population] , absorb(hbprov) vce(cluster provres)
outreg2 using "$output\robustness_cohabitations.xls" , keep(newEUxPostEU) append 



**************************************************
*         COHABITATIONS + MARRIAGES              * 

use "$data\Gain_marriage_robust.dta"  , clear
drop if year<=1997 
drop if year>=2003 & year<=2007
replace year=2001 if year<=2002
replace year=2011 if year>=2008
tab year

keep year hbc wbc provres region nm
append using "$data\cohabitations_tomerge.dta"

collapse (sum) nm , by( hbc wbc provres year)
lab var nm "marriage+cohabitation"

merge m:1 provres hbc year using "$data\Msingle_cohabitation_FINAL.dta" , keep(match master) keepusing(Msingle)
tab _merge
drop _merge 
merge m:1 provres wbc year using "$data\Fsingle_cohabitation_FINAL.dta" , keep(match master) keepusing(Fsingle)
tab _merge
drop _merge 
merge m:1 provres using "$data\population_byprovince.dta" , keep(match master) 
tab _merge
drop _merge

gen phixy = ln((nm^2)/(Fsingle*Msingle))

* Treatment
keep if wbc==0
gen nat= hbc
merge m:1 nat using "$data\CodiciStatiEsteri.dta" , keepusing(EUII)
tab _merge
drop _merge
drop if year==.
replace EUII=4 if nat==224 
replace EUII=4 if nat==258  
replace EUII=4 if nat==259  
replace EUII=4 if nat==260  
replace EUII=4 if nat==261  
replace EUII=6 if nat==317  
replace EUII=0 if nat==0 
lab define eui3  0 "ITA" 1 "EU15"  2 "EU2004" 3 "EU2007" 4 "EU_Other" 5 "Africa" 6 "Asia" 7 "America_South" 8 "OECD" 
label values EUII eui3
  
tab EUII, gen(euii)
rename euii1 ita
rename euii2 eu15
rename euii3 eu2004
rename euii4 eu2007
rename euii5 euother
rename euii6 africa
rename euii7 asia
rename euii8 americas
rename euii9 oecd
codebook EUII
drop if EUII==.

* Post and Interaction 
gen post2004= (year>=2004)
gen post2007= (year>=2007)
gen eu2004post2004= eu2004*post2004
gen eu2007post2007= eu2007*post2007
drop if phixy==.
drop if phixy==0
xtset provres 
order provres year
sort provres year

gen eg=hbc
merge m:1 provres eg year using "$data\Fsingle_cohabitation_FINAL_merge2.dta" , keep(match master) keepusing(Fsingle_for_hbc)
tab _merge
drop _merge 
replace Fsingle_for_hbc=round(Fsingle_for_hbc,1)
gen lnF=ln(Fsingle)
gen lnM=ln(Msingle)
gen lnFsingle_for_hbc=ln(Fsingle_for_hbc)

set matsize 5000
set more off
gen hbprov = hbc*1000+provres
egen newEUxPostEU=rmax(eu2004post2004 eu2007post2007)
gen yearprov = year*1000+provres

tab provres, gen(dprov)
forval i=1/`r(r)' {
gen provtrend`i' = dprov`i'*year
}

********************************************************************************
*  Table 5 - Column 6. Marriages and Cohabitations
*  Gains from marriage and cohabitation before and after the EU enlargements, DID estimates

areg phixy newEUxPostEU i.year provtrend1-provtrend`r(r)' [aw=population] , absorb(hbprov) vce(cluster provres)
outreg2 using "$output\robustness_cohabitations.xls" , keep(newEUxPostEU) append 






